|
|
|
|
|
|
|
Declare Function SendMessageBynum Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
ByVal lParam As Long) As Long
Declare Function SendMessageByString Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
ByVal lParam As String) As Long |
|
|
|
|
|
|
|
|
These functions provide safer ways to call the SendMessage function and reduce the likelihood of bugs due to incorrect parameter types. Using As Any in a declaration places all the responsibility for passing the correct parameter type on the person using the function instead of the person creating the declaration. Since the function is likely to be called multiple times, it's obvious that the As Any approach is more likely to result in errors than creating a type-safe declaration the first time around. |
|
|
|
|
|
|
|
|
Now that you've learned that the As Any declaration is evil and should be avoided at all costs, it may surprise you to see that I use it extensively throughout this book. The reason is simple: this book is intended to teach you how to declare and call even the most complex Win32 API function given only a C declaration to work with. The As Any parameter type forces you to think carefully about the parameter each time you call the function and thus will encourage you to learn and understand how the function is called. |
|
|
|
|
|
|
|
|
You'll also see that there are a few functions where you want the decision on parameter types and use of the ByVal keyword to be made each time the function is called. One good example of this is the RtlMoveMemory function. |
|
|
|
|
|
|
|
|
One of the most useful functions for understanding what gets passed as a parameter is the RtlMoveMemory function. This function copies memory from a source location to a destination location. What does this mean? |
|
|
|
|
|
|
|
|
A typical declaration for RtlMoveMemory is shown here: |
|
|
|
|
|
|
|
|
Private Declare Function RtlMoveMemory Lib "kernel32" (dest As Any, _
src As Any, ByVal Count As Long) As Long |
|
|
|
|
|
|
|
|
The function takes three parameters. The dest parameter specifies the destination for the copied memory. The src parameter specifies the source location of the data to copy. The Count parameter specifies the number of bytes to copy. |
|
|
|
|
|
|
|
|
Figure T5-3 shows the contents of the stack when the RtlMoveMemory function is called. The Count parameter is passed by value, so the corresponding stack entry contains the number of bytes to transfer. The other two parameters are declared here As Any, which means you can pass anything to the function. |
|
|
|
|
|